package com.basic.minio.controller;

import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.enums.BusinessType;
import com.basic.minio.domain.FileQO;
import com.basic.minio.service.IFileDownloadService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/business/fileDownload")
public class FileDownloadController extends BaseController {

    @Resource
    private IFileDownloadService fileDownloadService;

    /**
     * 下载文件（分片）
     */
    @Log(title = "单文件下载", businessType = BusinessType.DOWNLOAD)
    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadMultipartFile(FileQO fileQO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return fileDownloadService.downloadMultipartFile(fileQO, request, response);
    }

    /**
     * 获取分片url
     */
    @Log(title = "文件分片", businessType = BusinessType.OTHER)
    @GetMapping("/getChunkUrl")
    public AjaxResult getChunkUrl(@RequestParam(value = "chunkNum", defaultValue = "0") Integer chunkNum, @RequestParam("fileName") String fileName, @RequestParam(required = false, value = "filePath") String filePath, @RequestParam(value = "contentType", required = false) String contentType) {
        return success(fileDownloadService.getChunkUrl(chunkNum, fileName, filePath, contentType));
    }

    /**
     * 文件合并
     */
    @Log(title = "文件合并", businessType = BusinessType.OTHER)
    @GetMapping("/merge")
    public AjaxResult merge(@RequestParam(value = "chunkNum",defaultValue = "0") Integer chunkNum,
                            @RequestParam("fileName") String fileName,
                            @RequestParam(value = "contentType", required = false) String contentType,
                            @RequestParam("uploadId") String uploadId,
                            @RequestParam("fileMd5") String fileMd5,
                            @RequestParam("filePath") String filePath){
        return fileDownloadService.merge(chunkNum, fileName, contentType, uploadId, fileMd5, filePath);
    }
}

